package cn.wolfcode.luowowo.acticle.service.impl;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.Region;
import cn.wolfcode.luowowo.acticle.mapper.DestinationMapper;
import cn.wolfcode.luowowo.acticle.mapper.RegionMapper;
import cn.wolfcode.luowowo.acticle.query.DestinationQueryObject;
import cn.wolfcode.luowowo.acticle.query.DestinationThemeQueryObject;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 区域服务实现类
 */
@Service
//@Transactional 分布式事务操作项目四教
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageInfo query(DestinationQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list = destinationMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Destination> getDestinationByDeep(Integer deep) {
        return destinationMapper.selectDestinationByDeep(deep);
    }

    @Override
    public List<Destination> getDestinationByRegionId(Long rid) {
        //需要执行一个分支,国内分支
        if(rid == -1){
            return destinationMapper.selectDestByParentId(1L);
        }
        //获取区域对象
        Region region = regionMapper.selectByPrimaryKey(rid);
        //获取区域对象的所有关联地区id数组refIds
        Long[] refIds = region.getRefIds();
        return destinationMapper.selectDestByIds(refIds);
    }

    @Override
    public void changeHotValue(Integer hot, Long id) {
        destinationMapper.updateHotValue(hot, id);
    }

    @Override
    public void setInfo(String info, Long id) {
        destinationMapper.updateInfo(info, id);
    }

    @Override
    public void delete(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList<>();
        createToasts(parentId, list);
        Collections.reverse(list);
        return list;
    }

    private void createToasts(Long parentId, List<Destination> list) {
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if (destination == null){
            return;
        }
        list.add(destination);
        if(destination.getParent() != null){
            createToasts(destination.getParent().getId(), list);
        }
    }

    //查询当前目的地所在的国家
    public Destination getCountry(Long id) {
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if (dest.getDeep() == 1){
            return dest;
        }
        List<Destination> toasts = this.getToasts(id);
        if (toasts != null && toasts.size() > 0){
            return toasts.get(0);
        }
        return null;
    }

    //查询当前目的地所在的省份, 国外的返回null
    public Destination getProvince(Long id) {
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if(this.isBroad(id)){
            return null;
        }
        if (dest.getDeep() == 1){ //国家
            return null;
        }else if(dest.getDeep() == 2){ //省份
            return dest;
        }else if(dest.getDeep() == 3){ //城市
            return dest.getParent();
        }
        return null;
    }

    //国内地区按拼音集合数据
    public List<Map<String, Object>> getDestLeterMapInChina() {
        List<Destination> allDest = destinationMapper.selectDestLeterMapInChina();
        List<Map<String, Object>> lastList = new ArrayList<>();

        //遍历集合allDest,查询出所有拼音为a开头的地区, 加到A对应的map中, 同时完成b-z对应的map, 最后将有地区值的map加到lastLost中
        char lowerCase = 'a';
        char upperCase = 'A';
        //int count = 0;
        for (int i = 0; i < 26; i++) {
            Map<String, Object> map = new HashMap<>();
            ArrayList<Destination> list = new ArrayList<>();
            for (Destination destination : allDest) {
                String str = destination.getPinyin().substring(0, 1);
                if (String.valueOf(lowerCase).equals(str)){
                    //if(count < 8){
                        list.add(destination);
                        //count++;
                    //}
                }
            }
            map.put("letter", upperCase);
            map.put("dests", list);
            //count = 0;
            lowerCase++;
            upperCase++;
            if (list.size() > 0){
                lastList.add(map);
            }
        }
        return lastList;
    }

    //热门国内旅游目的地
    public List<Map<String, Object>> getHotDestinationsInChina() {
        List<Map<String, Object>> lastList = new ArrayList<>();
        this.setHotDestinations(lastList, new Long[]{3386L,3391L,3388L,3397L,3398L}, "云南",new int[]{0});
        this.setHotDestinations(lastList, new Long[]{3356L,3358L,3365L,3367L,3361L,3357L}, "四川");
        this.setHotDestinations(lastList, new Long[]{3409L,3412L,3414L,3416L,3411L}, "陕西");
        this.setHotDestinations(lastList, new Long[]{3167L,3163L,3169L,3168L}, "安徽",new int[]{1});
        this.setHotDestinations(lastList, new Long[]{3296L,3297L}, "海南");
        this.setHotDestinations(lastList, new Long[]{3135L,3136L,3139L,3144L,3137L}, "江苏");
        this.setHotDestinations(lastList, new Long[]{3446L,3448L,3447L}, "新疆");
        this.setHotDestinations(lastList, new Long[]{3254L,3247L,3252L}, "湖南");
        this.setHotDestinations(lastList, new Long[]{3185L,3184L,3187L}, "江西");
        this.setHotDestinations(lastList, new Long[]{3284L,3290L,3282L,3286L}, "广西",new int[]{0,1,2,3});
        this.setHotDestinations(lastList, new Long[]{3379L,3378L,3377L}, "贵州");

        return lastList;
    }

    public void setHotDestinations(List<Map<String, Object>> lastList, Long[] ids, String province, int...index){
        HashMap<String, Object> map = new HashMap<>();
        List<Destination> dests = destinationMapper.selectDestByIds(ids);
        for (int i : index) {
            dests.get(i).setIshot(true);
        }
        map.put("province", province);
        map.put("dests", dests);
        lastList.add(map);
    }

    //热门海外旅游目的地
    public List<Map<String, Object>> getHotDestinationsInAbroad() {
        List<Map<String, Object>> lastList = new ArrayList<>();
        this.setHotDestinations(lastList, new Long[]{273L,274L,272L}, "港台澳");
        this.setHotDestinations(lastList, new Long[]{2318L,2332L,2330L}, "泰国");
        this.setHotDestinations(lastList, new Long[]{83L,122L,164L,183L,193L,201L,217L,153L,139L,96L,51L,44L}, "亚洲");
        this.setHotDestinations(lastList, new Long[]{19L,65L,128L,219L}, "大洋洲",new int[]{0});
        this.setHotDestinations(lastList, new Long[]{39L,49L,50L,55L,58L,84L,66L,228L,231L,134L,30L,5L,93L}, "欧美",new int[]{6});
        this.setHotDestinations(lastList, new Long[]{6L,227L,11L,107L,144L,149L,199L,233L}, "中东非");

        return lastList;
    }

    //查询国内热门旅游出发地
    public List<Destination> getHotStartPlacesInChina() {
        return destinationMapper.selectHotStartPlacesInChina();
    }

    //根据旅游时间/旅游天数/主题查询地区
    public Map getDestinationBy3Condition(DestinationThemeQueryObject qo) {
        int pageSize = qo.getPageSize();
        int currentPage = qo.getCurrentPage();
        if(currentPage < 1){
            currentPage = 1;
            qo.setCurrentPage(1);
        }

        Map<String, Object> map = new HashMap<>();
        List<Destination> dests = destinationMapper.selectDestinationBy3Condition(qo);
        if(qo.getTravelTime() == -1){
            int pages = dests.size()%pageSize == 0 ? dests.size()/pageSize : dests.size()/pageSize + 1;
            if(dests.size() >pageSize && dests.size() > pageSize*currentPage){
                dests = dests.subList((currentPage -1)*pageSize, (currentPage -1)*pageSize + pageSize);
            }
            map.put("list", dests);
            map.put("pages", pages);
            map.put("total", dests.size());
            if(currentPage > pages){
                map.put("pageNum", pages);
            }else{
                map.put("pageNum", currentPage);
            }
            return map;
            //return dests;
        }


        List<Destination> list = new ArrayList<>();
        for (Destination dest : dests) {
            List<Long> travelTimes = dest.getTravelTimes();
            if(travelTimes.contains(qo.getTravelTime())){
                list.add(dest);
            }
        }
        int pages = list.size()%pageSize == 0 ? list.size()/pageSize : list.size()/pageSize + 1;
        if(list.size() >pageSize && list.size() > pageSize*currentPage){
            list = list.subList((currentPage - 1) * pageSize, (currentPage - 1) * pageSize + pageSize);
        }
        map.put("list", list);
        map.put("pages", pages);
        map.put("total", list.size());
        if(currentPage > pages){
            map.put("pageNum", pages);
        }else{
            map.put("pageNum", currentPage);
        }
        return map;
        //return list;
    }

    @Override
    public List<Destination> getDestinationByDeepIs3() {
        return destinationMapper.selectDestinationByDeepIs3();
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    @Override
    public boolean isBroad(Long id) {
        //做法1: 使用吐司方法
        /*List<Destination> destinations = getToasts(id);
        String name = destinations.get(0).getName();
        System.out.println(name);
        if("中国".equals(name)){
            return false;
        }
        return true;*/

        //做法2: 递归
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        if(destination == null){
            return false;
        }
        if("中国".equals(destination.getName())){
            return false;
        }
        Destination parent = destination.getParent();
        if(parent != null){
            Destination dest = destinationMapper.selectByPrimaryKey(parent.getId());
            if("中国".equals(dest.getName())){
                return false;
            }
        }else{ //顶级地区为空, 返回true
            return true;
        }
        return isBroad(parent.getId());
    }
}
